library PlayerSelectsHeroCommand initializer init requires LegacySystem, AmmoAPI

	globals
		private trigger MarineCasualtyTrigger
	endglobals
	
	function IsUnitHeroMarine takes unit u returns boolean
		local integer i = GetUnitTypeId(u)
		return /*
		*/ i=='Hpal' or i=='H000' or i=='H007' or i=='H003' or i=='H002' or i=='H00O' or /*
		*/ i=='H005' or i=='H00N' or i=='H006' or i=='H00J' or i=='H004' or i=='H008'

	endfunction

	private function MakeUnavailable takes integer uid returns nothing
		local integer i = 1
		loop
			exitwhen i>udg_Players
			call SetPlayerTechMaxAllowed(ConvertedPlayer(udg_Sorted[i]),uid,0)
			set i=i+1
		endloop
	endfunction

	private function TavernUpdate takes unit u returns nothing
		local integer uid
		local integer pid = GetConvertedPlayerId(GetOwningPlayer(u))
		set uid = GetUnitTypeId(u)
		call SetI("Shop",uid,GetI("Shop",uid)-1)
		if GetI("Shop",uid)<=0 then
			call MakeUnavailable(uid)
		endif
		if udg_MBcolors[pid]==0 and udg_Mode!=3 then
			set udg_MBcolors[pid] = 4
		endif
	endfunction

	private function MarineCasualty takes nothing returns nothing
		local integer A = 1
		local unit u = GetTriggerUnit()
		local player p = GetOwningPlayer(u)
		local player killer = GetOwningPlayer(GetKillingUnit())
		local integer i = GetConvertedPlayerId(p)
		local integer i2
		local integer i3
		local integer delayedNV = 0
		local real x = GetUnitX(u)
		local real y = GetUnitY(u)
		call ForceRemovePlayer(udg_EchoCompany,p)
		call ForceAddPlayer(udg_EchoCasualties,p)
		loop
			exitwhen A>udg_InitialPlayers
			if ConvertedPlayer(udg_Sorted[A])!=p then
				call SetPlayerAllianceBJ(ConvertedPlayer(udg_Sorted[A]),ALLIANCE_SHARED_VISION,true,p)
			endif
			set A=A+1
		endloop
		set udg_MiniMarineKills[i] = GetPlayerScore(p,PLAYER_SCORE_UNITS_KILLED)
		set udg_NightVision[i] = false
		if udg_Lighting==false then
			call NightVision(p,udg_NightVision[i])
		else
			set delayedNV=1
		endif
		call PlayerDied(i)
		if GetUnitTypeId(u)=='H002' then
			call putInteger(null,"NumCorpsman", (getInteger(null,"NumCorpsman") - 1))
		elseif GetUnitTypeId(u)=='H007' then
			call putBoolean(null,"ENGPresent",false)
		endif

		if GetUnitTypeId(u)=='H005' then
			set i3 = 2
		else
			set i3 = 3
		endif
		set i2 = GetPrimaryAmmoMagazines(p)/i3
		if i2>15 then
			set i2 = 15
		endif
		set A=1
		loop
			exitwhen A>i2
			call CreateItem('I006',x,y)
			call TriggerSleepAction(0.)
			set A=A+1
		endloop
		if GetUnitTypeId(u)!='h00E' then
		   call CreateItem('I018',x,y) //Marine Corpse
		endif
		call PolledWait(0.3)
		if GetUnitTypeId(u)!='h00E' then
			set udg_soundpath = "war3mapImported\\Casualty.mp3"
		call PlaySoundPath.execute()
		endif
		
		call AddPrimaryAmmo(u, -GetPrimaryAmmoMagazines(p), -GetPrimaryAmmoRounds(p))
		
		call SetPlayerAllianceStateBJ(p,Player(13),bj_ALLIANCE_UNALLIED)
		call SetPlayerAllianceStateBJ(p,Player(14),bj_ALLIANCE_UNALLIED)
		call ResetToGameCameraForPlayer(p,2.5)
		set udg_FPSMode[i] = false
		if FPSCheck() then
			//call DisableTrigger(gg_trg_First_Person_Mode)
		endif
		if udg_Players<1 and udg_DMDeliveryTime[101]<1337.0 then
			set udg_DMDeliveryTime[101] = 1337.0
			//call EnableTrigger(gg_trg_ShowStats)
			call PolledWait(5.)
			set i3=1
			loop
				exitwhen i3>udg_InitialPlayers
				// mos
				set i3=i3+1
			endloop
			call DisplayTimedTextToPlayer( GetLocalPlayer(), 0, 0, 120.0, "Third Platoon, Echo Company has been eliminated. Enter the NOTD Aftermath website at www.notd-aftermath.com." )
		endif
		

		call PolledWait(5.)
		set udg_Reloading[i] = 0
	  
		if udg_Experience[i]>=500 then
			call CreatePet(p, x, y)
		elseif udg_Players>1 then
			call DisplayTimedTextToPlayer(p,0.0,0.0,20.0,"|cffcd950cALICE|r : Your Marine has died and you will receive reinforcements later. Learn the fundamentals and discuss NOTD Aftermath at www.notd-aftermath.com")
		endif
		if delayedNV==1 then
			loop
				exitwhen delayedNV==0
				call PolledWait(3.)
				if udg_Lighting==false then
					call NightVision(p,udg_NightVision[i])
					set delayedNV=0
				endif
			endloop
		endif
		set u = null
		call TriggerSleepAction(25.)
		if GetWidgetLife(udg_Pet[i])>0.405 then
			call UnitRemoveAbility(udg_Pet[i],'A062')
		endif
	endfunction

	function InitHero takes unit u returns nothing
		
		local player p = GetOwningPlayer(u)
		local real x
		local real y
		local integer pid = GetConvertedPlayerId(p)
		local integer fireteam = 1
		
		if(pid>5) then
			set fireteam = 2
		endif 
		set udg_HeroSelected[pid] = true
		set udg_Hero[pid] = u
		
		call TavernUpdate(u)
		call DisplayTimedTextToPlayer(GetLocalPlayer(),0.0,0.0,2.0, "|cff1e90ff" + GetPlayerName(p) + "|r has picked " + GetUnitName(u) ) 
		
		set x = udg_SquadLeaderX[fireteam]+GetRandomReal(-350.0,350.0)
		set y = udg_SquadLeaderY[fireteam]+GetRandomReal(-350.0,350.0)
		call SetUnitPosition(u,x,y)
		call PanCameraToTimedForPlayer(p,x,y,0.0)

		if not(IsTerrainPathable(x,y,PATHING_TYPE_FLOATABILITY)) then
			call DestroyEffect(AddSpecialEffect("Objects\\Spawnmodels\\Naga\\NagaDeath\\NagaDeath.mdl",x,y))
		else
			call DestroyEffect(AddSpecialEffect("Objects\\Spawnmodels\\Undead\\ImpaleTargetDust\\ImpaleTargetDust.mdl",x,y))
		endif

		set udg_soundpath = "war3mapImported\\Reload.wav"
		call PlaySoundPath.execute()
		call SelectUnitForPlayerSingle(u,p)

		call TriggerRegisterUnitEvent(MarineCasualtyTrigger, u, EVENT_UNIT_DEATH)
		call RegisterUnitForAilmentChecks(u)
		call SetPlayerState(p,PLAYER_STATE_RESOURCE_FOOD_CAP,0)

		if GetPlayerTechCount(p,'Rhpt',true)>3 then
			call UnitAddAbility(u,'A004') //Leadership
		endif

		set p = null

	endfunction

	private function MarineHeroSelection takes nothing returns nothing
		local player p
		local unit u
		local unit u2
		local integer i
		local integer i2
		local integer id
		local integer bonus = 0
		local integer fireteam = 1
		local integer mos
		local real x 
		local real y
		local boolean randomselect = false
		local string s = GetEventPlayerChatString()
	   
		
		set u = GetSoldUnit()
		set p = GetOwningPlayer(u)
		set id = GetConvertedPlayerId(p)
		if id>5 then
			set fireteam = 2
		endif

		call RemoveUnit(GetBuyingUnit())

		set i = GetUnitTypeId(u)

		//if i=='H00N' then
		 //   call putBoolean(null,"UAVPresent",true)
		//    set udg_HeroAvailable[7] = 0
		//    call RemoveUnit(u)
		//    set u = CreateUnit(p,'h00E',14589.0,7752.0,240.0)
	   // endif

		
		set p = GetOwningPlayer(u)
		set id = GetConvertedPlayerId(p) 
	 
		set udg_HeroSelected[id] = true

		//if i=='H00N' then
		//    set udg_FuelEfficiency[id] = 1.0
		//    call SetI("Shop",'H00N',0)
		//    call MakeUnavailable('H00N')
		//    set udg_PlayerIcon[id] = "ReplaceableTextures\\CommandButtons\\BTN_Unit_Predator.blp"
		///    call PredatorMQ3Init(u)
		//    return
	   // endif

		//if bonus==1 then
			//call AddMagazines(p,3)
		//endif


		if udg_Mode==2 then
			call UnitAddAbility(u,'A062')
			call UnitModifySkillPoints(u,1)
		endif

		
		if udg_Mode==2 and udg_Experience[id]>=10000 and udg_Experience[id]<50000 then
			call SetPlayerTechResearched(p,'R00P',1)
		endif
		if udg_Experience[id]>65000 then
			call IncUnitAbilityLevel(u,'A06H') //Colt
			call IncUnitAbilityLevel(u,'A07P') //Desert Eagle
		endif
		if udg_InitialPlayers<3 then
			set udg_KillFactor[id] = (udg_KillFactor[id]+1.0)/2.0
		endif
		set u = null
		set u2 = null
	endfunction

	private function init takes nothing returns nothing
		local trigger t
		set t=CreateTrigger()
		call TriggerRegisterPlayerUnitEvent(t, Player(11), EVENT_PLAYER_UNIT_SELL, null)
		call TriggerAddAction(t,function MarineHeroSelection)
		
		set t = CreateTrigger()
		call TriggerAddAction(t,function MarineCasualty)
		set MarineCasualtyTrigger = t
	endfunction

endlibrary